<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input
          v-model="state.listQuery.username"
          style="width: 200px"
          class="filter-item"
          placeholder="用户名或昵称"
          @keyup.enter="handleList"
      />
      <el-input
          v-model="state.listQuery.accountNumber"
          style="width: 200px"
          class="filter-item"
          placeholder="账号"
          @keyup.enter="handleList"
      />
      <el-button
          v-waves
          class="filter-item"
          type="primary"
          @click="handleList"
          clearable
      >
        <el-icon style="vertical-align: middle">
          <Search/>
        </el-icon>
        搜索
      </el-button>
    </div>
    <el-table
        v-loading="state.listLoading"
        :data="state.list"
        :load="state.listLoading"
        border
        style="width: 100%">
      <el-table-column
          label="账号"
          prop="accountNumber"
          align="center"
          width="100">
      </el-table-column>
      <el-table-column
          label="注册时间"
          prop="createTime"
          align="center"
          width="100">
        <template #default="scope">
          <span v-text="formatDate(scope.row.createTime)"/>
        </template>
      </el-table-column>
      <el-table-column
          label="用户名"
          prop="username"
          align="center"
          width="140">
      </el-table-column>
      <el-table-column
          label="昵称"
          prop="nickname"
          align="center"
          width="150">
      </el-table-column>
      <el-table-column
          label="手机号"
          prop="phone"
          align="center"
          width="110">
      </el-table-column>
      <el-table-column
          label="性别"
          prop="gender"
          align="center"
          width="55">
        <template #default="scope">
          <span v-if="scope.row.gender === 1">男</span>
          <span v-else-if="scope.row.gender === 2">女</span>
          <span v-else>未知</span>
        </template>
      </el-table-column>
      <el-table-column
          label="生日"
          prop="birthday"
          align="center"
          width="100">
        <template #default="scope">
          <span v-text="formatDate(scope.row.birthday)"/>
        </template>
      </el-table-column>
      <el-table-column
          label="星座"
          prop="constellation"
          align="center"
          width="70">
      </el-table-column>
      <el-table-column
          label="地区"
          prop="area"
          align="center"
          width="200">
        <template #default="scope">
          <span v-text="scope.row.country"/>
          <span v-if="scope.row.province != null">&nbsp;&nbsp;</span>
          <span v-if="scope.row.province != null" v-text="scope.row.province"/>
          <span v-if="scope.row.city != null">&nbsp;&nbsp;</span>
          <span v-if="scope.row.city != null" v-text="scope.row.city"/> 
        </template>
      </el-table-column>
      <el-table-column
          label="职业"
          prop="job"
          align="center"
          width="120">
      </el-table-column>
      <el-table-column
          label="简介"
          prop="introduction"
          align="center"
          min-width="250">
      </el-table-column>
      <el-table-column
          label="状态"
          prop="status"
          width="60"
          align="center">
        <template #default="scope">
          <el-tag
              class="status-tag"
              size="small"
              style="background-color: #b3e19d; width: 30px"
              v-if="scope.row.status != null && scope.row.status === true">
          </el-tag>
          <el-tag
              class="status-tag"
              size="small"
              style="background-color: #c45656; width: 30px"
              v-else>
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="100" align="center">
        <template #default="scope">
          <a v-if="scope.row.status === true">
            <el-button size="small"
                       @click="handleSuspended(scope.row)"
            >封禁
            </el-button>
          </a>
          <a v-else>
            <el-button size="small"
                       @click="handleActivate(scope.row)"
            >解封
            </el-button>
          </a>
        </template>
      </el-table-column>
    </el-table>
    <div style="margin-top:10px">
      <el-pagination
          :total="state.total"
          v-model:page="state.listQuery.pageNum"
          v-model:limit="state.listQuery.pageSize"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="state.listQuery.pageNum"
          :page-sizes="[10, 20]"
          layout="total, sizes, prev, pager, next, jumper"
      />
    </div>
  </div>
</template>

<script setup>
import {
  reactive,
  onMounted
} from 'vue'
import {get, put, handleRegexpTxt} from '@/utils/http'
import {apis} from '@/apis'
import moment from 'moment'

const state = reactive({
  tableKey: 0,
  list: [],
  total: 0,
  listLoading: true,
  listQuery: {
    pageNum: 1,
    pageSize: 10,
    username: undefined,
    accountNumber: undefined
  }
})


const handleList = () => {
  state.listLoading = true
  get(apis.ACCOUNT, {params: state.listQuery}).then((res) => {
    console.log(res)
    state.list = res.data
    state.total = res.page.total
    state.listQuery.pageNum = res.page.current
    state.listQuery.pageSize = res.page.size
    state.listLoading = false
  })
}

const changeStatus = (row) => {
  let activateUmsAdmin = handleRegexpTxt(apis.ACTIVATE_UMS_ADMIN, '{id}', row.id);
  let suspendedUmsAdmin = handleRegexpTxt(apis.SUSPENDED_UMS_ADMIN, '{id}', row.id);
  console.log(row.status)
  if (row.status) {
    put(activateUmsAdmin)
  } else {
    put(suspendedUmsAdmin)
  }
}

const formatDateTime = (value) => {
  if (value === null) {
    return value
  }
  return moment(value).format('YYYY-MM-DD HH:mm:ss');
}

const formatDate = (value) => {
  if (value === null) {
    return value
  }
  return moment(value).format('YYYY-MM-DD');
}

const handleSuspended = (row) => {
  let account = handleRegexpTxt(apis.SUSPENDED_ACCOUNT, '{id}', row.id);
  put(account).then(() => {
    handleList()
  })
}

const handleActivate = (row) => {
  let account = handleRegexpTxt(apis.ACTIVATE_ACCOUNT, '{id}', row.id);
  put(account).then(() => {
    handleList()
  })
}

const handleSizeChange = (val) => {
  state.listQuery.pageSize = val
  handleList()
}

const handleCurrentChange = (val) => {
  state.listQuery.pageNum = val
  handleList()
}

onMounted(() => {
  handleList()
})

</script>

<style>
.role-tag {
  margin-right: 2px;
}
</style>


